package com.keepers.repair.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import com.keepers.repair.R;
import com.keepers.repair.adapter.TaskTodayAdapter;
import com.keepers.repair.helper.ConsValue;
import com.keepers.repair.helper.SpHelper;
import com.keepers.repair.model.GetTaskListModel;
import com.keepers.repair.model.UserModel;
import com.keepers.repair.net.DataLoader;
import com.keepers.repair.utils.AlertToast;
import com.keepers.repair.view.PullRecyclerView;
import com.keepers.repair.view.TitleBar;

/**
 * auth aboom
 * date 2018/11/10
 */
public class TaskListActivity extends BaseActivity {

    public static final int TO_DAY      = 0;
    public static final int TO_TOMORROW = 1;

    private int              type;
    private UserModel        loginUserInfo;
    private TextView         emptyView;
    private TaskTodayAdapter taskAdapter;
    private PullRecyclerView recyclerView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_task);
        loginUserInfo = SpHelper.getLoginUserInfo();
        type = getIntent().getIntExtra(ConsValue.INTENT_KEY, 0);
        TitleBar titleBar = f(R.id.title_bar);
        titleBar.initializeHeader(getBarTitle());
        emptyView = f(R.id.empty_view);
        recyclerView = f(R.id.list);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        taskAdapter = new TaskTodayAdapter(R.layout.item_tasktoday);
        taskAdapter.setOnItemClickListener((adapter, view, position) -> {
            GetTaskListModel item = taskAdapter.getItem(position);
            if (item != null) {
                item.type = type;
                Intent intent = new Intent(this, TaskListDetailActivity.class);
                intent.putExtra(ConsValue.INTENT_KEY, item);
                startActivity(intent);
            }
        });
        recyclerView.setAdapter(taskAdapter);
        recyclerView.setOnRefreshListener(this::loadData);
    }

    @Override
    protected void onResume() {
        super.onResume();
        loadData();
    }

    private void loadData() {
        recyclerView.showRefresh();
        rxHelper.pend(DataLoader.getTaskList(loginUserInfo.GroupGid, loginUserInfo.Gid, type, getTaskListByIDModel -> {
            recyclerView.loadComplete(true);
            if (getTaskListByIDModel == null || getTaskListByIDModel.Task == null) {
                AlertToast.show("获取" + getBarTitle() + " 任务发生异常...");
                return;
            }
            taskAdapter.setNewData(getTaskListByIDModel.Task);
            emptyView.setVisibility(getTaskListByIDModel.Task.size() == 0 ? View.VISIBLE : View.GONE);
        }));
    }

    private String getBarTitle() {
        if (type == TO_DAY) {
            return "今日任务";
        }
        if (type == TO_TOMORROW) {
            return "明日任务";
        }
        return "";
    }

}
